// 给定一个二叉树的根节点 root ，返回 它的 中序 遍历 。
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */

  function TreeNode(val, left, right) {
      this.val = (val===undefined ? 0 : val)
      this.left = (left===undefined ? null : left)
      this.right = (right===undefined ? null : right)
  }
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
// 中序遍历
 var preorderTraversal = function(root) {
    if(!root){
        return [];
    }
    const stack = [];
    const res = [];
    let curr;
    stack.push(root);
    while(curr || stack.length){
        curr = stack.pop();
        while(curr.left){// 一直遍历到最左的左子树
            stack.push(curr);
            curr = curr.left;
        }
        res.push(curr.val);
        if(curr.right)   stack.push(curr.right);
    }
    return res
};

let TreeNode1 = TreeNode(1,null,2);
let TreeNode2 = TreeNode(3);
console.log(TreeNode1);
// TreeNode1.right.left = TreeNode2;
let root = [1,null,2,3];
console.log(preorderTraversal(TreeNode1));